package com.china_goldcard.topbarview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 导航视图
 */

public class TopBarView extends RelativeLayout{

    // 接口
    public interface NavBtnOnClickListener{
        void leftBtnClicked(View view);
        void rightBtnClicked(View view);
        void centerBtnClicked(View view);
    }
    private TextView titleView;
    private TextView leftTxt;
    private TextView rightTxt;
    private View line;
    private NavBtnOnClickListener clickListener;
    public NavBtnOnClickListener getClickListener() {
        return clickListener;
    }
    public void setClickListener(NavBtnOnClickListener clickListener) {
        this.clickListener = clickListener;
    }
    private String titleTextStr;
    private int titleTextSize ;
    private int  titleTextColor ;
    private String leftBtnStr;
    private int leftColor;
    private int leftSize;
    private String rightBtnStr;
    private int rightColor;
    private int rightSize;
    private boolean hideLine = false;
    private Drawable leftImage ;
    private Drawable rightImage ;
    private Drawable bgColor;
    public TopBarView(Context context){
        this(context,null);
    }
    public TopBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        getConfig(context, attrs);
        initView(context);
    }
    /**
     * 从xml中获取配置信息
     */
    private void getConfig(Context context, AttributeSet attrs) {
        //TypedArray是一个数组容器用于存放属性值
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.TopBarView);
        titleTextStr = attributes.getString(R.styleable.TopBarView_titleText);
        titleTextColor = attributes.getColor(R.styleable.TopBarView_titleColor, Color.WHITE);
        titleTextSize = attributes.getDimensionPixelSize(R.styleable.TopBarView_titleSize,(int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
        leftImage = attributes.getDrawable(R.styleable.TopBarView_leftImage);
        rightImage = attributes.getDrawable(R.styleable.TopBarView_rightImage);
        leftBtnStr =  attributes.getString(R.styleable.TopBarView_leftTxt);
        rightBtnStr =  attributes.getString(R.styleable.TopBarView_rightTxt);
        bgColor = attributes.getDrawable(R.styleable.TopBarView_bgColor);
        leftColor = attributes.getColor(R.styleable.TopBarView_leftTxtColor,Color.WHITE);
        leftSize = attributes.getDimensionPixelSize(R.styleable.TopBarView_leftSize,(int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics()));
        rightColor = attributes.getColor(R.styleable.TopBarView_rightTxtColor,Color.WHITE);
        rightSize = attributes.getDimensionPixelSize(R.styleable.TopBarView_rightSize,(int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 14, getResources().getDisplayMetrics()));
        hideLine = attributes.getBoolean(R.styleable.TopBarView_hideLine,hideLine);
        //用完务必回收容器
        attributes.recycle();
    }


    /**
     * 初始化视图
     * @param context
     */
    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.top_bar_view,this,true);
        titleView = (TextView) findViewById(R.id.centerTitleV);
        leftTxt = (TextView) findViewById(R.id.leftTxt);
        rightTxt = (TextView) findViewById(R.id.rightTxt);
        line = (View) findViewById(R.id.nav_line);
        leftTxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null){
                    clickListener.leftBtnClicked(v);
                }
            }
        });
        rightTxt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null){
                    clickListener.rightBtnClicked(v);
                }
            }
        });
        titleView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null){
                    clickListener.centerBtnClicked(v);
                }
            }
        });
        this.updateUI();
    }

    /**
     * 更新ui
     */
    private void updateUI(){
        this.setBackground(bgColor);
        if (hideLine){
            line.setVisibility(View.GONE);
        }else{
            line.setVisibility(View.VISIBLE);
        }
        titleView.setText(titleTextStr);
        titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX,titleTextSize);
        titleView.setTextColor(titleTextColor);
        leftTxt.setVisibility(View.GONE);
        rightTxt.setVisibility(View.GONE);
        if (null != leftImage) {
            leftTxt.setVisibility(View.VISIBLE);
            leftImage.setBounds(0, 0, leftImage.getMinimumWidth(), leftImage.getMinimumHeight());
            leftTxt.setCompoundDrawables(leftImage,null,null,null);
        }
        if (leftBtnStr!=null && leftBtnStr.trim().length() > 0){
            leftTxt.setVisibility(View.VISIBLE);
            leftTxt.setText(leftBtnStr.trim());
            leftTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX,leftSize);
            leftTxt.setTextColor(leftColor);
        }
        if (null != rightImage){
            rightImage.setBounds(0, 0, leftImage.getMinimumWidth(), leftImage.getMinimumHeight());
            rightTxt.setVisibility(View.VISIBLE);
            rightTxt.setCompoundDrawables(null,null,rightImage,null);
        }
        if (rightBtnStr!=null && rightBtnStr.trim().length() > 0){
            rightTxt.setVisibility(View.VISIBLE);
            rightTxt.setText(rightBtnStr.trim());
            rightTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX,rightSize);
            rightTxt.setTextColor(rightColor);
        }
    }
}
